package com.lvtulife.common.web.listener;

import com.lvtulife.common.component.constants.SysConstants;
import com.lvtulife.common.util.ConfigUtil;
import com.lvtulife.common.web.component.vo.SessionInfo;
import com.lvtulife.paas.system.service.SysOnlineServiceI;
import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.*;
import javax.servlet.http.*;


/**
 * 监听在线用户上线下线
 *
 * @author 孙宇
 */
public class OnlineListener implements ServletContextListener, ServletContextAttributeListener, HttpSessionListener, HttpSessionAttributeListener, HttpSessionActivationListener, HttpSessionBindingListener, ServletRequestListener, ServletRequestAttributeListener {

    private static final Logger logger = Logger.getLogger(OnlineListener.class);

    private static ApplicationContext ctx = null;

    public OnlineListener() {
    }

    public void requestDestroyed(ServletRequestEvent arg0) {
    }

    /**
     * 向session里增加属性时调用(用户成功登陆后会调用)
     */
    public void attributeAdded(HttpSessionBindingEvent evt) {
        String name = evt.getName();
        logger.debug("向session存入属性：" + name);
        if (ConfigUtil.getSessionInfoName().equals(name)) {// 如果存入的属性是sessionInfo的话
            HttpSession session = evt.getSession();
            SessionInfo sessionInfo = (SessionInfo) session.getAttribute(name);
            if (sessionInfo != null) {
                getSysonlineServiceBean().userOnlineRegister(sessionInfo.getUser().getLoginName(), sessionInfo.getUser().getIp(), SysConstants.SYS_OLTYPE_IN);
            }
        }
    }

    /**
     * 服务器初始化时调用
     */
    public void contextInitialized(ServletContextEvent evt) {
        ctx = WebApplicationContextUtils.getWebApplicationContext(evt.getServletContext());
    }

    public void sessionDidActivate(HttpSessionEvent arg0) {
    }

    public void valueBound(HttpSessionBindingEvent arg0) {
    }

    public void attributeAdded(ServletContextAttributeEvent arg0) {
    }

    public void attributeRemoved(ServletContextAttributeEvent arg0) {
    }

    /**
     * session销毁(用户退出系统时会调用)
     */
    public void sessionDestroyed(HttpSessionEvent evt) {
        HttpSession session = evt.getSession();
        if (session != null) {
            logger.debug("session销毁：" + session.getId());
            SessionInfo sessionInfo = (SessionInfo) session.getAttribute(ConfigUtil.getSessionInfoName());
            if (sessionInfo != null) {
                getSysonlineServiceBean().userOnlineRegister(sessionInfo.getUser().getLoginName(), sessionInfo.getUser().getIp(), SysConstants.SYS_OLTYPE_OUT);
            }
        }
    }

    public void attributeRemoved(HttpSessionBindingEvent arg0) {
    }

    public void attributeAdded(ServletRequestAttributeEvent evt) {
    }

    public void valueUnbound(HttpSessionBindingEvent arg0) {
    }

    public void sessionWillPassivate(HttpSessionEvent arg0) {
    }

    public void sessionCreated(HttpSessionEvent arg0) {
    }

    public void attributeReplaced(HttpSessionBindingEvent arg0) {
    }

    public void attributeReplaced(ServletContextAttributeEvent arg0) {
    }

    public void attributeRemoved(ServletRequestAttributeEvent arg0) {
    }

    public void contextDestroyed(ServletContextEvent evt) {
        logger.debug("服务器关闭");
    }

    public void attributeReplaced(ServletRequestAttributeEvent arg0) {
    }

    public void requestInitialized(ServletRequestEvent arg0) {
    }

    public SysOnlineServiceI getSysonlineServiceBean() {
        return (SysOnlineServiceI) ctx.getBean("SysOnlineService");
    }
}
